package com.itheima.tlias84.controller;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias84.Util.ResultUtil;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/cookie")
public class TestCookieController {

    @GetMapping("save")
    public ResultUtil saveCookie(HttpServletResponse response){
        Cookie cookie = new Cookie("username", "spring");
        response.addCookie(cookie);
        Cookie cookie1 = new Cookie("password", "123456");
        response.addCookie(cookie1);
        return ResultUtil.success();
    }
    @GetMapping("get")
    public ResultUtil getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        JSONObject jsonObject = new JSONObject();
        for (Cookie cookie : cookies) {
            String username = cookie.getAttribute("username");
            jsonObject.put("username",username);
            String password = cookie.getAttribute("password");
            jsonObject.put("password",password);
        }
         return ResultUtil.success(jsonObject);

    }

}
